declare boundary-space preserve;

(:
Part 2 / Q5
The QID of questions on the quiz that fewer than half the students answered,
or that fewer than half of those who answered actually got right.
:)

let $bank_questions := fn:doc("bank.xml")/QuestionBank/Question
let $quiz_questions :=
    for $qid in fn:doc("quiz.xml")/Quiz/Question/@QID
    return <Question QID="{$qid}"
            answer="{$bank_questions/*[@QID eq $qid]/@answer}"/>

let $students := fn:doc("class.xml")/ClassResponses/Student
let $num_students := count($students)

for $q in $quiz_questions
let $correct := count($students/QuestionResponse[@QID = $q/@QID and
                      @Response = $q/@answer])
where (2 * $correct) lt $num_students
return $q/@QID
